package id.co.shuba.spamobile.data;

import id.co.shuba.spamobile.App;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import android.database.Cursor;
import android.util.Log;

public class BaseDB {
	protected final DatabaseHandler _db;
	public static final String FULL_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss"; 
	public static SimpleDateFormat dateFormat = new SimpleDateFormat(FULL_DATE_TIME_FORMAT, Locale.US);
	
	public static String getCurrentDateTime() {
		//return formatFullDate(new Date());
		return App.getProcessDateTime();
	}
	
	public static String getSysDateTime() {
		return formatFullDate(new Date());
	}
	
	public static Date parseDate(String date) {
		try {
			return dateFormat.parse(date);
		} catch (Exception e) {
			return null;
		}
	}
	
	protected static String formatFullDate(Date date) {
		return dateFormat.format(date);
	}
	
	public BaseDB(DatabaseHandler db) {
		_db = db;
	}
	 
	public static String cursorGetString(Cursor cursor, String columnName) {
		return cursor.getString(cursor.getColumnIndex(columnName));
	}
	
	public static Date cursorGetDate(Cursor cursor, String columnName) {
		int index = cursor.getColumnIndex(columnName);
		if (cursor.isNull(index))
			return null;					
		
		String value = cursor.getString(index);
		Date date = null;
		try {
			date = dateFormat.parse(value);
		} catch (ParseException e) {
			Log.e("Parsing Date", e.getMessage());
		}
		return date;
	}
}
